package com.luoluo.view;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.luoluo.bean.Document;
import com.luoluo.dao.DocumentDao;
import com.luoluo.lucene.SearchHelper;
import com.luoluo.search.SearchTools;

public class ViewTools {
	private DocumentDao dao;
	private static BeanFactory factory = new ClassPathXmlApplicationContext(
			"classpath:/applicationContext*.xml");

	public ViewTools() {
		dao = (DocumentDao) factory.getBean("documentDao", DocumentDao.class);
	}

	public List<Document> findDocument(String key, boolean title, int page,
			int size) {
		if (null == key)
			return null;
		if (page < 1)
			page = 1;
		if (size <= 0)
			size = 10;
		List<Long> docids = SearchTools.findDocument(key, false, page, size);
		if (null == docids)
			return null;
		return dao.select(docids.toArray(new Long[docids.size()]));
	}

	public static void test() {
		ViewTools vt = new ViewTools();
		System.out.println(vt.findDocument("java", false, 1, 10));
	}

	public String abbreviate(String text, int width) {
		return StringUtils.abbreviate(text, width);
	}

	public String highlight(String text, String key) {
		return SearchHelper.highlight(text, key);
	}
}
